package com.sweet.entity;

import com.sweet.enums.MessageType;
import io.vertx.core.json.JsonObject;

/**
 * {
 * "type" : "消息类型",
 * "id" : "消息ID",
 * "content" : "消息内容",
 * "to" : "接收者",
 * "from" : "发送人",
 * "token" : "令牌",
 * "time" : "100001"
 * }
 */
public class MessageBody {
  private final MessageType type;
  private final String id;
  private final String content;
  private final String to;
  private final String from;
  private final String token;
  private final String time;

  public JsonObject toJson() {
    return new JsonObject()
        .put("type", type.name())
        .put("id", id)
        .put("content", content)
        .put("to", to)
        .put("from", from)
        .put("token", token)
        .put("time", time);
  }

  public static MessageBody fromJson(JsonObject jsonObject) {
    return new MessageBody(
            MessageType.TEXT.name().equals(jsonObject.getString("type"))
                    ? MessageType.TEXT :
                (MessageType.ACK.name().equals(jsonObject.getString("type"))
                    ? MessageType.ACK : MessageType.PING),
            jsonObject.getString("id"),
            jsonObject.getString("content"),
            jsonObject.getString("to"),
            jsonObject.getString("from"),
            jsonObject.getString("token"),
            jsonObject.getString("time")
    );

  }

  public MessageBody(MessageType type, String id, String content, String to, String from, String token, String time) {
    this.type = type;
    this.id = id;
    this.content = content;
    this.to = to;
    this.from = from;
    this.token = token;
    this.time = time;
  }

  public MessageType getType() {
    return type;
  }

  public String getId() {
    return id;
  }

  public String getContent() {
    return content;
  }

  public String getTo() {
    return to;
  }

  public String getFrom() {
    return from;
  }

  public String getToken() {
    return token;
  }

  public String getTime() {
    return time;
  }

  @Override
  public String toString() {
    return "MessageBody{" +
            "type=" + type +
            ", id='" + id + '\'' +
            ", content='" + content + '\'' +
            ", to='" + to + '\'' +
            ", from='" + from + '\'' +
            ", token='" + token + '\'' +
            ", time='" + time + '\'' +
            '}';
  }


}
